const Reader = require('../models/Reader');

// 获取当前登录用户的个人信息
exports.getProfile = async (ctx) => {
  try {
    const reader = await Reader.findByPk(ctx.state.user.id, {
      attributes: { exclude: ['password'] } // 不返回密码
    });
    if (!reader) {
      ctx.status = 404;
      ctx.body = { error: 'Reader not found' };
      return;
    }
    ctx.body = reader;
  } catch (error) {
    ctx.status = 500;
    ctx.body = { error: error.message };
  }
};

// 更新当前登录用户的个人信息
exports.updateProfile = async (ctx) => {
  try {
    const reader = await Reader.findByPk(ctx.state.user.id);
    if (!reader) {
      ctx.status = 404;
      ctx.body = { error: 'User not found' };
      return;
    }
    // 只允许更新指定的字段
    const { name, phone, email, address, avatar, introduction, role } = ctx.request.body;
    await reader.update({ name, phone, email, address, avatar, introduction, role });
    ctx.body = { message: 'Profile updated successfully' };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { error: error.message };
  }
};